
from __future__ import print_function
import math

def distance_3_points(A, B, interm):
	return A.compute_distance(interm) + B.compute_distance(interm)

def distance(point1, point2):
        distx = point1.x - point2.x
        disty = point1.y - point2.y
        return math.sqrt(distx*distx+disty*disty)


class Point(object):

        def __init__(self, name, x, y):
                self.name = name
                self.x = x
                self.y = y

        def compute_distance(self, point):
                return distance(self, point)

class Route(object):
        def __init__(self, start_point, end_point, required_steps, max_x, max_y):
                self.start_point = start_point
                self.end_point = end_point
                self.current_point = start_point
                self.required_steps = required_steps
                self.waypoints = []
		self.max_x, self.max_y = max_x , max_y
		self.leng = 0

        def current_length(self):
                leng = 0
                if len(self.waypoints) > 0:
                        leng = distance(self.start_point,self.waypoints[0])
                for idx in range(1,len(self.waypoints)):
                        leng += distance(self.waypoints[idx-1],self.waypoints[idx])
                if len(self.waypoints) == self.required_steps:
                        leng + distance(self.waypoints[-1] , self.end_point)
                return leng

        def recompute_length(self):
                leng = distance(self.start_point,self.waypoints[0])
                for idx in range(1,len(self.waypoints)):
                        leng += distance(self.waypoints[idx-1],self.waypoints[idx])
		if len(self.waypoints) == self.required_steps:
	                leng += distance(self.waypoints[-1] , self.end_point)
		self.leng = leng
                return leng

	def length(self): 
                if len(self.waypoints) != self.required_steps:
                        return self.max_x**2 + self.max_y**2
		return self.leng + distance(self.current_point,self.end_point)

        def add_waypoint(self, point):
                self.waypoints.append(point)
		self.leng += distance(self.current_point, point)
		self.current_point = point
                pass

        def remove_last_waypoint(self):
		if len(self.waypoints): self.waypoints.remove(self.waypoints[-1])

        def remove_last_waypoints(self, start_index):
		if len(self.waypoints) >= start_index: 
			self.waypoints = self.waypoints[:start_index]
			if start_index != 0: 
				self.recompute_length()
				self.current_point = self.waypoints[-1]
			else: 
				self.current_point = self.start_point
		else: raise BaseException("remove_last_waypoints : asking to remove points and keep more points than route has")

        def print_route(self):
                print("A({0},{1})->".format(self.start_point.x,self.start_point.y),end="")
                for p in self.waypoints:
                        print("{0}({1},{2})->".format(p.name, p.x, p.y),end="")
                for miss in range(len(self.waypoints),self.required_steps):
                        print("?->",end="")
                print("B({0},{1}) : {2}".format(self.end_point.x,self.end_point.y,self.length()))



